<template>
    <div>
        <Card>
            <!-- search start -->
            <div v-if="is_show_search" class="search">
                <Form :model="search" class="search_box" inline label-position="right">

                    <FormItem label="部门：">
                        <Select class="option" v-model="search.department">
                            <Option value="全部"> 全部 </Option>
                            <Option v-for="item in demList " :value="item" :key="item">{{ item }}</Option>
                        </Select>
                    </FormItem>

                    <FormItem label="姓名：">
                        <Input v-model="search.username"></Input>
                    </FormItem>


                    <FormItem label="时间：">
                        <DatePicker style="width:300px" type="datetimerange" placeholder="Select date and time" @on-change="changeTime($event)"></DatePicker>
                    </FormItem>

                    <FormItem>
                        <Button icon="md-search" type="primary" @click="searchList">搜索</Button>
                    </FormItem>

                </Form>

            </div> <!-- end search -->

            <Table ref="table" :loading="loading" stripe :columns="columns1" :data="data1"></Table>
            <Page :total="total" :current="search.page" show-sizer show-total :page-size="search.rows" @on-page-size-change="getListByPageSize" @on-change="getListByPageNum"> </Page>
        </Card>

    </div>
</template>

<script>
    import axios from '@/libs/api.request'
    import {
        mapGetters,
        mapMutations
    } from 'vuex'
    export default {
        name: 'mistake-matter',
        data() {
            return {
                loading: false,

                is_show_search: true,

                // 搜索对象
                search: {
                    username: '',
                    start_time: '',
                    end_time: '',
                    department: '全部',
                    flag: '',
                    page: 1,
                    rows: 10
                },

                // 数据总条数
                total: 0,

                columns1: [{
                        type: 'index',
                        width: 60,
                        align: 'center'
                    },

                    {
                        title: '部门',
                        width: 120,
                        align: 'center',
                        key: 'department'
                    },

                    {
                        title: '姓名',
                        width: 120,
                        align: 'center',
                        key: 'username'
                    },

                    {
                        title: '日期',
                        width: 180,
                        align: 'center',
                        key: 'now_date',
                    },
                    {
                        title: '说明',
                        minWidth: 450,
                        align: 'left',
                        key: 'content',
                    },
                    {
                        title: '操作',
                        align: 'center',
                        width: 180,
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'primary',
                                        size: 'default',
                                        icon: 'ios-eye'
                                    },
                                    style: {
                                        marginRight: '14px',
                                        height: '30px'
                                    },
                                    on: {
                                        click: () => {
                                            this.myClose('matter-detail')
                                            this.$_changePage('matter-detail', '事项详情', {
                                                matterid: params.row.matterid
                                            })
                                        }
                                    }
                                }, '事项详情'),

                            ])
                        }
                    }
                ],
                data1: []
            }
        },
        methods: {
            ...mapMutations(['myClose']),
            ...mapGetters(['getDem', 'getRole']),

            // 获取知错能改-事项明细列表
            get_mistake_matter() {
                this.loading = true
                let {
                    ...data
                } = this.search

                axios.ajax_post('admin-mistake-ajax_get_sx', data).then(res => {
                    this.loading = false
                    if (res.data.code === 1) {
                        this.data1 = res.data.data.list
                        this.total = res.data.data.total_num

                        if (this.data1.length < 1 && this.total > 0) {
                            this.search.page = Math.ceil(this.total / this.search.rows)
                            this.get_mistake_matter()
                        }
                    } else {
                        this.$Notice.error({
                            title: res.data.msg,
                            duration: 3
                        })
                    }
                })

            },

            // 搜索
            searchList() {
                this.search.page = 1
                this.search.rows = 10
                this.get_mistake_matter()
            },

            // 格式化时间
            changeTime(value) {
                this.search.start_time = value[0]
                this.search.end_time = value[1]
            },

            getListByPageSize(rows) {
                this.search.rows = rows
                this.get_mistake_matter()
            },

            getListByPageNum(page) {
                this.search.page = page
                this.get_mistake_matter()
            },
        },
        computed: {
            demList() {
                return this.getDem()
            },
            role() {
                return this.getRole()
            }
        },
        created() {
            if (this.$route.params.flag) {
                this.search.flag = this.$route.params.flag
                this.is_show_search = false
            }
            this.get_mistake_matter()
        },
        activated() {
            if (this.$route.params.flag) {
                this.search.flag = this.$route.params.flag
                this.is_show_search = false
            }
            this.get_mistake_matter()
        }
    }
</script>

<style lang='less' scoped>
    .search {
        .search_box {
            display: flex;

            .option {
                width: 150px;
            }

            .ivu-form-item {
                margin-right: 12px;
                display: flex;
                justify-content: flex-start;
                margin-bottom: 20px;
            }
        }
    }
</style>